package org.zjvis.datascience.common.util;

import com.google.common.collect.Lists;
import org.zjvis.datascience.common.model.AggregateColumn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description 查询数据包装工具类
 * @date 2021-10-29
 */
public class DataUtil {

    public static List<Object[]> wrapDataFromAggHead(ResultSet rs, List<AggregateColumn> heads) throws SQLException {
        List<Object[]> data = Lists.newArrayList();
        while (rs.next()) {

            Object[] record = new Object[heads.size()];

            for (int i = 0; i < heads.size(); i++) {
                Object o = rs.getObject(heads.get(i).getIndex());
                record[i] = o;
            }
            data.add(record);
        }

        return data;
    }

    public static List<Object[]> wrapDataFromAggHead(List<Map> rawData, List<AggregateColumn> heads) throws SQLException {
        List<Object[]> data = Lists.newArrayList();
        for (Map map: rawData) {

            Object[] record = new Object[heads.size()];

            for (int i = 0; i < heads.size(); i++) {
                Object o = map.get(heads.get(i).getName());
                record[i] = o;
            }
            data.add(record);
        }

        return data;
    }

    public static void main(String[] args) {
//        Long day = 1000L * 60 * 60 * 24;
//        Long month = day * 30;
//        Long year = day * 365;
//        Date date = new Date();
//
//        for (int i = 0; i < 10000; i++) {
//            long time = date.getTime();
//            time = time + day * i;
//            System.out.println(formatChineseDateStr(new Date(time)));
//        }
        System.out.println(formatChineseDateStr(new Date(System.currentTimeMillis())));


    }

    public static String formatChineseDateStr(Date date) {
        return formatChineseDateStr(date, "yyyy-M");
    }

    /**
     * @param date 时间
     * @return 汉字年月日
     */
    public static String formatChineseDateStr(Date date, String dateFormat) {
        String format = new SimpleDateFormat(dateFormat).format(date);
        String[] split = format.split("-"); //期待是 年-月-日
        StringBuffer stringBuffer = new StringBuffer();
        String strYear = getStrYear(split[0]);
        String strMonth = getStrMonth(split[1]);
        stringBuffer.append(strYear + "年" + strMonth + "月");
        if (split.length > 2) {
            stringBuffer.append(getStrDay(split[2])+ "日");
        }

        return stringBuffer.toString();
    }

    /**
     * @param day 数字日 13
     * @return 汉字日 13==>十三
     */
    private static String getStrDay(String day) {
        int dayLength = day.length();
        String strDay = "";
        if (dayLength < 2) {
            strDay = numToStr(day);
            return strDay;
        } else {
            if (day.startsWith("1")) {
                if (day.equals("10")) {
                    return "十";
                } else {
                    String substring = day.substring(1);
                    strDay = "十" + numToStr(substring);
                    return strDay;
                }
            } else if (day.startsWith("2")) {
                if (day.equals("20")) {
                    return "二十";
                } else {
                    String substring = day.substring(1);
                    strDay = "二十" + numToStr(substring);
                    return strDay;
                }
            } else if (day.startsWith("3")) {
                if (day.equals("30")) {
                    return "三十";
                } else {
                    String substring = day.substring(1);
                    strDay = "三十" + numToStr(substring);
                    return strDay;
                }
            }
        }
        return "";
    }


    /**
     * @param month 数字月 7
     * @return 中文月 7==>七
     */
    private static String getStrMonth(String month) {
        return numToStr(month);
    }

    /**
     * @param year 数字年 2020
     * @return 汉字年 二零二零
     */
    private static String getStrYear(String year) {
        int length = year.length();
        String strYear = "";
        for (int i = 0; i < length; i++) {
            String substring = year.substring(i, i + 1);
            strYear = strYear + numToStr(substring);
        }
        return strYear;
    }

    /**
     * @param nun 0~9 ==>零~九
     * @return
     */
    private static String numToStr(String nun) {
        if (nun.equals("0")) {
            return "〇";
        } else if (nun.equals("1")) {
            return "一";
        } else if (nun.equals("2")) {
            return "二";
        } else if (nun.equals("3")) {
            return "三";
        } else if (nun.equals("4")) {
            return "四";
        } else if (nun.equals("5")) {
            return "五";
        } else if (nun.equals("6")) {
            return "六";
        } else if (nun.equals("7")) {
            return "七";
        } else if (nun.equals("8")) {
            return "八";
        } else if (nun.equals("9")) {
            return "九";
        } else if (nun.equals("10")) {
            return "十";
        } else if (nun.equals("11")) {
            return "十一";
        } else if (nun.equals("12")) {
            return "十二";
        }
        return "";
    }

}
